package com.karrysong.answerapplication.beans;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.Nullable;

import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

public class DBMaster extends SQLiteOpenHelper {

    private static String name = "questions.db"; //表示数据库的名称
    private static int version = 1;
    public DBMaster(@Nullable Context context) {
        super(context, name, null, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String sql = "CREATE TABLE tb_question (objectId varchar(255), _index INTEGER, type INTEGER, typeIndex INTEGER, questionType INTEGER, title varchar(255), aOption varchar(255), bOption varchar(255), cOption varchar(255), dOption varchar(255), answer varchar(255), analysis varchar(255))";
        db.execSQL(sql);

        //查询全部非会员题库
        BmobQuery<TK> tkBmobQuery = new BmobQuery<>();
        tkBmobQuery.addWhereEqualTo("isVip", false);
        tkBmobQuery.findObjects(new FindListener<TK>() {
            @Override
            public void done(List<TK> list, BmobException e) {
                if (e == null && list != null){
                    for (TK tk : list) {
                        BmobQuery<Question> bmobQuery = new BmobQuery<>();
                        bmobQuery.addWhereEqualTo("questionType", tk.getTk_number());
                        bmobQuery.setLimit(500);
                        bmobQuery.findObjects(new FindListener<Question>() {
                            @Override
                            public void done(List<Question> list, BmobException e) {
                                if (e != null || list == null || list.size() == 0){
                                    return;
                                }
                                int sum = 0;
                                for (Question question : list) {
                                    ContentValues values = new ContentValues();
                                    values.put("objectId", question.getObjectId());
                                    values.put("_index", question.getIndex());
                                    values.put("type", question.getType());
                                    values.put("typeIndex", question.getTypeIndex());
                                    values.put("questionType", question.getQuestionType());
                                    values.put("title", question.getTitle());
                                    values.put("aOption", question.getaOption());
                                    values.put("bOption", question.getbOption());
                                    values.put("cOption", question.getcOption());
                                    values.put("dOption", question.getdOption());
                                    values.put("answer", question.getAnswer());
                                    values.put("analysis", question.getAnalysis());
                                    db.insert("tb_question", null, values);
                                }
                            }
                        });
                    }
                }
            }
        });


    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}
